import { MessageBoard } from '@/entity/message-board.entity';
import { User } from '@/entity/user.entity';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { MessageBoardDto } from './dto/message-board.dto';

@Injectable()
export class MessageBoardService {
  constructor(
    @InjectRepository(MessageBoard)
    private readonly messageBoardRepositry: Repository<MessageBoard>,
  ) {}

  async getMessageBoardList(limit: number) {
    const qb = this.messageBoardRepositry.createQueryBuilder('messageBoard');
    const result = await qb
      .leftJoinAndMapOne(
        'messageBoard.userInfo',
        User,
        'users',
        'users.id = messageBoard.user_id',
      )
      .limit(limit)
      .getMany();
    return result;
  }

  async addMessage(messageBoardDto: MessageBoardDto) {
    const messageBoard = new MessageBoard();
    messageBoard.content = messageBoardDto.content;
    messageBoard.userId = messageBoardDto.userId;
    return this.messageBoardRepositry.save(messageBoard);
  }
}
